﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class ZbAppraisalList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int SubID = HelperURL.GetIntFromUrlString("ID");
                LoadMyAppraisal(SubID);
            }
        }

        private void LoadMyAppraisal(int SubID)
        {
            //查该用户是否是专家用户
            //得到专家ID
            //查询评分项目列表

            string UserID = DBController.GetInstance().GetAuthenticatedUserID();
            AP_User u = new AP_User();
            u.LoadByPrimaryKey(UserID);
            int UmpID = u.UmpID;


            VIEW_KP_ENTRY vwKpEntry = new VIEW_KP_ENTRY();
            //vwKpEntry.Where.UmpID.Value = UmpID;
            //vwKpEntry.Where.KpStatus.Value = KPFacade.APPRAISAL_STATUS_OF_PENDING;
            //增加主题或考评表验证有效性
            vwKpEntry.Where.SubjectID.Value = SubID;
            //vwKpEntry.Where.SubjectEnable.Value = "Y";
            //vwKpEntry.Where.SubjectBeginDate.Value = DateTime.Now;
            //vwKpEntry.Where.SubjectBeginDate.Operator = MyGeneration.dOOdads.WhereParameter.Operand.LessThanOrEqual;
            //vwKpEntry.Where.SubjectEndDate.Value = DateTime.Now;
            //vwKpEntry.Where.SubjectEndDate.Operator = MyGeneration.dOOdads.WhereParameter.Operand.GreaterThanOrEqual;

            vwKpEntry.Query.AddOrderBy(VIEW_KP_ENTRY.ColumnNames.KpID, WhereParameter.Dir.ASC);
            vwKpEntry.Query.AddOrderBy(VIEW_KP_ENTRY.ColumnNames.UmpName, WhereParameter.Dir.ASC);
            vwKpEntry.Query.Load();

            HelperDatagrid.BindAndSort(vwKpEntry.DefaultView, dgAppraisal, VIEW_KP_ENTRY.ColumnNames.KpID, VIEW_KP_ENTRY.ColumnNames.KpID);
        }

        protected void dgAppraisal_ItemDataBound1(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //HelperDatagrid.RowClick(e, "#" , "", "");

                //Label lbDeptName = (Label)e.Item.Cells[3].FindControl("lbDeptName");
                Label lbStatus = (Label)e.Item.Cells[6].FindControl("lbStatus");
                HyperLink lnkAppraisal = (HyperLink)e.Item.Cells[7].Controls[0];

                DataRow row = ((DataRowView)e.Item.DataItem).Row;
                int KpType = Int32.Parse(row[VIEW_KP_ENTRY.ColumnNames.KP_Category_ID].ToString());
                int UmpType = Int32.Parse(row[VIEW_KP_ENTRY.ColumnNames.UmpType].ToString());
                lbStatus.Text = KPFacade.GetStatusMessage(Int32.Parse(row[VIEW_KP_ENTRY.ColumnNames.KpStatus].ToString()));
                if (Int32.Parse(row[VIEW_KP_ENTRY.ColumnNames.KpStatus].ToString()) == KPFacade.APPRAISAL_STATUS_OF_PENDING
                    || Int32.Parse(row[VIEW_KP_ENTRY.ColumnNames.KpStatus].ToString()) == KPFacade.APPRAISAL_STATUS_OF_START)
                {
                    string SubID = e.Item.Cells[0].Text;
                    string KpID = e.Item.Cells[1].Text;
                    string UmpID = e.Item.Cells[2].Text;
                    //string sUrl = String.Format("ZbFormAppraisal.aspx?subid={0}&kpid={1}&umpid={2}&Display=1", SubID, KpID, UmpID);
                    string sUrl = @"../" + String.Format(KPFacade.GetKpDisplyURL(KpType), SubID, KpID, UmpID);
                    if (UmpType == 1)
                    {
                        //指标填报表
                        sUrl = String.Format("ZbFromAppraisal2.aspx?subid={0}&kpid={1}&umpid={2}&Display=1", SubID, KpID, UmpID);
                    }
                    lnkAppraisal.Attributes.Add(ConstantController.JS_ONCLICK, HelperURL.GetShowWindowModalStringWithParams(sUrl, 1024, 768));
                    //lnkAppraisal.Attributes.Add(ConstantController.JS_ONCLICK, HelperURL.GetShowWindowString(sUrl));
                }
                else
                {
                    lnkAppraisal.Visible = false;
                }

                //删除评分
                LinkButton lnkDel = (LinkButton)e.Item.Cells[8].Controls[0];
                lnkDel.Attributes.Add(ConstantController.MSG_ALERT_ACTION, ConstantController.MSG_ALERT_SCRIPT);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("ID");
            HelperURL.Redirect("../Appraisal/SubjectDisplay.aspx?ID=" + SubID);
        }

        protected void dgAppraisal_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SubID = int.Parse(dgAppraisal.SelectedItem.Cells[0].Text);
            int KpID = int.Parse(dgAppraisal.SelectedItem.Cells[1].Text);
            int UmpID = int.Parse(dgAppraisal.SelectedItem.Cells[2].Text);
            string ItemIDs = "";
            VW_ZB_ItemResult vw = new VW_ZB_ItemResult();
            vw.Where.SubID.Value = SubID;
            vw.Where.KpID.Value = KpID;
            vw.Where.UmpID.Value = UmpID;
            vw.Query.Load();
            while (!vw.EOF)
            {
                ItemIDs += vw.ResultID.ToString() + ",";
                vw.MoveNext();
            }
            ItemIDs += "-1";

            KP_Item_Result result = new KP_Item_Result();
            result.Where.KP_Result_ID.Value = ItemIDs;
            result.Where.KP_Result_ID.Operator = WhereParameter.Operand.In;
            result.Query.Load();
            while (!result.EOF)
            {
                result.Item_Score = 0;
                result.MoveNext();
            }

            KP_Subject_Table_Ump u = new KP_Subject_Table_Ump();
            u.Where.SubjectID.Value = SubID;
            u.Where.KpTableID.Value = KpID;
            u.Where.UmpID.Value = UmpID;
            u.Where.UmpType.Value = 0;
            if (u.Query.Load())
            {
                u.Status = KPFacade.APPRAISAL_STATUS_OF_PENDING;
                u.Save();
            }

            result.Save();

            HelperMessage.Message("清除评分完成！");
            Label lbStatus = (Label)dgAppraisal.SelectedItem.Cells[6].FindControl("lbStatus");
            lbStatus.Text = KPFacade.GetStatusMessage(0);
        }
    }
}
